import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

// 引入总路由入口文件 
import router  from './routers/index'

//引入UI框架样式
import 'vant/lib/index.css';
// 引入UI框架
import Vant from 'vant';


createApp(App)
.use(router)
.use(Vant) //挂载UI组件
.mount('#app')


// 添加路由守卫做判断是否有无登录
router.beforeEach(async(to,from,next) =>{
    if(to.meta.auth){
        const { cookies } = useCookies();
        //判断是否有无cookie 
        let LoginUser = cookies.get('LoginUser')
        if (!LoginUser || JSON.stringify(LoginUser) == '{}'){
            showToast('未登陆,请先去登陆');
            next('/business/base/login')
            return
        } else {
            //获取id
            let id = LoginUser.id ?  LoginUser.id : 0;
            let mobile = LoginUser.mobile ? LoginUser.mobile : ''

            //接口请求 校验用户ID
            let result = await Api.check({id: id, mobile: mobile})

            if(result.code == 1){
                // 重新存储用户的信息
                cookies.set('LoginUser', result.data)
                next()
            } else {
                // 删除存储的用户信息
                cookies.remove('LoginUser')
                next('/business/base/login')
                return
            }
        }
    } else {
        next()
    }
})